<?php

/* @project : Template
 * @auther  : 青云
 * @date    : 2018/1/10
 * @desc    : 用户控制器
 */

namespace app\admin\controller;

use think\Request;
use app\common\model\Excel;
use app\common\model\Activity as ActivityModel;
use app\common\model\ActivitySignUp as ActivitySignUpModel;


class ActivitySignUp extends Base
{
    public function index(Request $request)
    {
        $aid = $request->aid ?? '';
        $username = $request->username ?? '';

        if ($request->isAjax()) {
            $where[] = ['aid', 'eq', $aid];
            $where[] = ['is_del', 'eq', 0];
            if (!empty($request->username)) {
                $where[] = ['username', 'like', '%' . $username . '%'];
            }

            $this->_search_filter($where);

            $model = new ActivitySignUpModel();
            $list = $this->_list($model, $where);
            return json($list);
        }

        $this->assign('aid', $aid);

        return $this->fetch();
    }

    function export_to_excel()
    {
        $params = input();
        $aid = $params['aid'] ?? '';
        $map = [];
        $map[] = ['aid', 'eq', $aid];
        $this->_search_filter($map);

        $activity_name = (new ActivityModel())->where('id', $aid)->value('title') ?? '';

        $excel = new Excel();

        $excel->load('static/excel/sign_data.xlsx');
        
        // 设置运行内存及超时时间
        ini_set('memory_limit', '2048M');
        ini_set('max_execution_time', '300');

        $cols[] = array('title' => '报名ID', 'field' => 'id');
        $cols[] = array('title' => '报名时间', 'field' => 'create_time');
        $cols[] = array('title' => '姓名', 'field' => 'username');
        $cols[] = array('title' => '身份证号', 'field' => 'id_card_no');
        $cols[] = array('title' => '手机号', 'field' => 'mobile');
        $cols[] = array('title' => '邮箱', 'field' => 'email');

        
        if ($aid == 1) {
            $cols[] = array('title' => '毕业院校', 'field' => 'graduated_school');
            $cols[] = array('title' => '学历', 'field' => 'education');
            $cols[] = array('title' => '毕业年度', 'field' => 'graduation_year');
            $cols[] = array('title' => '意向行业', 'field' => 'industry_type');
            $cols[] = array('title' => '求职岗位', 'field' => 'job_type');
            $cols[] = array('title' => '期望薪资', 'field' => 'expected_salary');
        } else {
            $cols[] = array('title' => '学校/公司', 'field' => 'school_name');
            $cols[] = array('title' => '参会目的', 'field' => 'purpose');
        }


        $excel->cols($cols);

        $model = new ActivitySignUpModel();

        $data = $model->where($map)->select();

        foreach ($data as $key => $value) {
            $data[$key]['activity_name'] = $activity_name;
        }

        $excel->data($data);

        $date = date('Y-m-d_H:i');

        $file_name = $activity_name . '_报名表_' . $date . '.xlsx';

        $excel->download($file_name);
    }
}
